package com.sunlei.webtest.quickstart.fanoutexchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
 * @Author: sun
 * @Description:
 * @Data: create in 17:43 2018/11/22
 * @Modfied by:
 */
public class FanoutProducter {
            public static void main(String[] args) throws IOException, TimeoutException {

                System.out.println("-----Fanout Exchange Producter strat-----");
                /** 1.创建一个connectionFactory 并进行配置*/
                ConnectionFactory connectionFactory = new ConnectionFactory();
                connectionFactory.setHost("127.0.0.1");
                connectionFactory.setPort(5672);
                connectionFactory.setVirtualHost("/");
                /** 2.通过连接工厂创建连接 */
                Connection connection =  connectionFactory.newConnection();
                /** 3.通过connection创建channel*/
                Channel channel = connection.createChannel();
                /** 4.通过channel发送数据
                 *  @1 exchange :exchange名称,如果为空默认为"\",此时的规则是找有没有与routingkey同名的队列进行投递
                 *  @2 routingkey
                 *  The default exchange is implicitly bound to every queue, with a routing key equal to the queue name.
                 *  It is not possible to explicitly bind to, or unbind from the default exchange. It also cannot be deleted.
                 * */


                final String exchangeName = "test_fanout_exchange";
                channel.basicPublish(exchangeName,"",null,"fanout rabbitmq, save".getBytes());
                channel.basicPublish(exchangeName,"",null,"fanout rabbitmq, update".getBytes());
                channel.basicPublish(exchangeName,"",null,"fanout rabbitmq, delete uid".getBytes());
                /** 5.记得记得关闭连接*/
                channel.close();
                connection.close();
        }
}
